﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;

namespace WineStore.Controllers
{

    public class CategorySimController : Controller
    {
        // GET: /CategorySim/ShowCategorySim/
        public ViewResult ShowCategorySim(int? page)
        {

            using (var db = new WineDbContext())
            {
                var categories = db.CategorySims
                    .OrderByDescending(c => c.CategorySimId);

                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(categories.ToPagedList(pageNumber, pageSize));
            }
        }

        // Create CategorySim 
        [HttpGet]
        public ActionResult CreateCategorySim()
        {
            return View();
        }

        // Create CategorySim 
        [HttpPost]
        public ActionResult CreateCategorySim(CategorySim category)
        {
            bool flag = true;
            try
            {
                // Validate in server
                if (ModelState.IsValid)
                {
                    using (var db = new WineDbContext())
                    {
                        foreach (var item in db.Sims)
                        {
                            if (item.CategorySim.CategorySimName.ToUpper().Replace(" ","").Equals(category.CategorySimName.ToUpper().Replace(" ","")))
                            {
                                flag = false;
                                break;
                            }
                        }
                        if (flag == true)
                        {
                            db.Entry(category).State = EntityState.Added;
                            db.SaveChanges();
                            return RedirectToAction("ShowCategorySim", "CategorySim");
                        }
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

        // Show Detail CategorySim 
        [HttpGet]
        public ActionResult EditCategorySim(int id = 0)
        {
            CategorySim category = null;
            using (var db = new WineDbContext())
            {
                category = db.CategorySims.Find(id);
                if (category == null)
                {
                    return HttpNotFound();
                }
            }
            return View(category);
        }

        /* Khi phương thức SaveChanges được gọi, cờ Modified làm cho Entity Framework cập nhật dòng tương ứng
           trong CSDL. Tất cả các cột sẽ được cập nhật, bao gồm cả các cột người dùng không thay đổi.*/
        // Edit CategorySim 
        [HttpPost]
        public ActionResult EditCategorySim(CategorySim category)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new WineDbContext())
                    {
                        db.Entry(category).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("ShowCategorySim", "CategorySim");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

        // GET: /CategorySim/DeleteCategorySim/
        public ActionResult DeleteCategorySim(int id)
        {
            CategorySim category = null;
            using (var db = new WineDbContext())
            {
                category = db.CategorySims.Find(id);
                if (category == null)
                {
                    return HttpNotFound();
                }
            }
            return View(category);
        }

        // POST: /CategorySim/DeleteCategorySim/
        [HttpPost, ActionName("DeleteCategorySim")]
        public ActionResult DeleteConfirmed(int id)
        {
            CategorySim category = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    category = db.CategorySims.Find(id);
                    db.Entry(category).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowCategorySim", "CategorySim");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

    }
}
